package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.CompanyDocumentDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.CompanyDocument;
import org.springframework.stereotype.Repository;

@Repository
public class CompanyDocumentDaoImpl extends
		AbstractDaoImpl<CompanyDocument, Integer> implements CompanyDocumentDao {

	protected CompanyDocumentDaoImpl() {
		super(CompanyDocument.class);
	}

	@Override
	public void saveCompanyDocument(CompanyDocument companyDocument) {
		saveOrUpdate(companyDocument);
	}

	@Override
	public List<CompanyDocument> findCompanyDocument(int companyId) {
		return findByCriteria(Restrictions.eq("companyId", companyId));
	}

	public List<CompanyDocument> searchCompanyDocument(int companyId,
			Date fromDate, Date toDate) {
		Criteria criteria = getCurrentSession().createCriteria(
				CompanyDocument.class);

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<CompanyDocument> searchCompanyDocument(int companyId,
			Date fromDate, Date toDate, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				CompanyDocument.class);

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}
	
	
}
